package com.master.train.business.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.master.train.business.dao.TrainSeatDao;
import com.master.train.business.entity.TrainCarriageEntity;
import com.master.train.business.entity.TrainSeatEntity;
import com.master.train.business.enums.SeatColEnum;
import com.master.train.business.service.TrainCarriageService;
import com.master.train.business.service.TrainSeatService;
import com.master.train.common.utils.PageUtils;
import com.master.train.common.utils.Query;
import com.master.train.common.utils.SnowUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("trainSeatService")
public class TrainSeatServiceImpl extends ServiceImpl<TrainSeatDao, TrainSeatEntity> implements TrainSeatService {

    private static final Logger LOG = LoggerFactory.getLogger(TrainSeatService.class);
    @Autowired
    private TrainSeatDao trainSeatDao;
    @Autowired
    private TrainCarriageService trainCarriageService;


    @Transactional
    public void genTrainSeat(String trainCode) {



        DateTime now = DateTime.now();

        // 使用MP的条件构造器清空当前车次下的所有座位记录
        LambdaQueryWrapper<TrainSeatEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainSeatEntity::getTrainCode, trainCode);
        trainSeatDao.delete(queryWrapper);

        // 查找当前车次下的所有的车厢
        List<TrainCarriageEntity> carriageList = trainCarriageService.selectByTrainCode(trainCode);
        LOG.info("当前车次下的车厢数：{}", carriageList.size());

        // 循环生成每个车厢的座位
        for (TrainCarriageEntity trainCarriage : carriageList) {
            Integer rowCount = trainCarriage.getRowCount();
            String seatType = trainCarriage.getSeatType();
            int seatIndex = 1;

            // 根据车厢的座位类型，筛选出所有的列
            List<SeatColEnum> colEnumList =
                    com.master.train.business.enums.SeatColEnum.getColsByType(seatType);
            LOG.info("根据车厢的座位类型，筛选出所有的列：{}", colEnumList);

            // 使用MP的批量插入优化性能
            List<TrainSeatEntity> seatBatch = new ArrayList<>();
            // 循环行数
            for (int row = 1; row <= rowCount; row++) {
                // 循环列数
                for (com.master.train.business.enums.SeatColEnum seatColEnum : colEnumList) {
                    // 构造座位数据
                    TrainSeatEntity TrainSeatEntity = new TrainSeatEntity()
                            .setId(SnowUtil.getSnowflakeNextId())
                            .setTrainCode(trainCode)
                            .setCarriageIndex(trainCarriage.getIndex())
                            .setRow(StrUtil.fillBefore(String.valueOf(row), '0', 2))
                            .setCol(seatColEnum.getCode())
                            .setSeatType(seatType)
                            .setCarriageSeatIndex(seatIndex++)
                            .setCreateTime(now)
                            .setUpdateTime(now);
                    trainSeatDao.insert(TrainSeatEntity);
                }
            }
        }
    }



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TrainSeatEntity> page = this.page(
                new Query<TrainSeatEntity>().getPage(params),
                new QueryWrapper<TrainSeatEntity>()
        );

        return new PageUtils(page);
    }

}